home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Java / JavaRadioStation / Source / com / apple / jens / radio / SequentialMusicDirector.java < prev    next >
Encoding:
Java Source  |  2000-09-28  |  4.4 KB  |  122 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        SequentialMusicDirector.java
  3.     
  4.     Copyright:     © Copyright 1999-2000 Apple Computer, Inc. All rights reserved.
  5.     
  6.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  7.                 ("Apple") in consideration of your agreement to the following terms, and your
  8.                 use, installation, modification or redistribution of this Apple software
  9.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  10.                 please do not use, install, modify or redistribute this Apple software.
  11.  
  12.                 In consideration of your agreement to abide by the following terms, and subject
  13.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  14.                 copyrights in this original Apple software (the "Apple Software"), to use,
  15.                 reproduce, modify and redistribute the Apple Software, with or without
  16.                 modifications, in source and/or binary forms; provided that if you redistribute
  17.                 the Apple Software in its entirety and without modifications, you must retain
  18.                 this notice and the following text and disclaimers in all such redistributions of
  19.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  20.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  21.                 Apple Software without specific prior written permission from Apple.  Except as
  22.                 expressly stated in this notice, no other rights or licenses, express or implied,
  23.                 are granted by Apple herein, including but not limited to any patent rights that
  24.                 may be infringed by your derivative works or by other works in which the Apple
  25.                 Software may be incorporated.
  26.  
  27.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  28.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  29.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  31.                 COMBINATION WITH YOUR PRODUCTS.
  32.  
  33.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  34.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  35.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  36.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  37.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  38.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  39.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40.                 
  41.     Change History (most recent first):
  42.  
  43. */
  44.  
  45.  
  46. package com.apple.jens.radio;
  47.  
  48. import com.apple.jens.mp3.MP3File;
  49. import java.io.*;
  50. import java.util.Properties;
  51.  
  52.  
  53. /** A MusicDirector that plays all MP3 files in its directory in sequence.
  54.     It syncs with the current directory contents each time through, so it's
  55.     safe to add/remove/rename files while it's playing.
  56.     
  57.     <p>ShuffleMusicDirector subclasses this and overrides the <code>reset</code>
  58.     method to shuffle the track list each time the sequence starts.
  59. */
  60.     
  61. public class SequentialMusicDirector implements MusicDirector {
  62.  
  63.     /** Initializes the SequentialMusicDirector. */
  64.     public void initialize( MusicLibrary lib, Station station ) {
  65.         fLibrary = lib;
  66.         Radio.LOG(1,this,"initialized on "+lib);
  67.         fFiles = fLibrary.getTrackList();
  68.         reset();
  69.     }
  70.     
  71.  
  72.     /** Returns the next track to play. */
  73.     public synchronized MP3File getNextTrack( ) {
  74.         boolean alreadyReset = false;
  75.         MP3File track;
  76.         do{
  77.             while( ++fIndex >= fFiles.length ) {
  78.                 if( alreadyReset ) {
  79.                     // We're in an infinite loop: no playable files
  80.                     Radio.WARN(1,this,"Can't find any files to play! Giving up...");
  81.                     return null;
  82.                 }
  83.                 Radio.LOG(1,this,"End of playlist; restarting");
  84.                 sync();                // Sequential play syncs with dir only at the end
  85.                 reset();
  86.                 ++fIndex;
  87.                 alreadyReset = true;
  88.             }
  89.             track = fFiles[fIndex];
  90.         }while( !track.isMP3FileType() );
  91.         Radio.LOG(2,this,"Selected track "+track.getName());
  92.         return track;
  93.     }
  94.     
  95.     
  96.     /** Updates fFiles by calling <code>syncTrackList</code> on the MusicLibrary. */
  97.     protected boolean sync( ) {
  98.         if( fLibrary.syncTrackList() ) {
  99.             fFiles = fLibrary.getTrackList();
  100.             return true;
  101.         } else
  102.             return false;
  103.     }
  104.     
  105.     
  106.     /** Resets the iteration back to the beginning. */
  107.     protected void reset( ) {
  108.         fIndex = -1;
  109.     }
  110.     
  111.     
  112.     public String toString( ) {
  113.         return getClass().getName()+" on "+fLibrary;
  114.     }
  115.     
  116.     
  117.     protected MusicLibrary fLibrary;
  118.     protected MP3File[] fFiles;
  119.     protected int fIndex;
  120.     
  121. }
  122.